import edu.rit.ds.registry.AlreadyBoundException;
import edu.rit.ds.registry.NotBoundException;
import edu.rit.ds.registry.RegistryProxy;
import java.rmi.NoSuchObjectException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

/**
 * @author Alan Kaminsky
 * @source P2Pedia example
 * 
 * @contributor Praneeth Pulusani
 * Class Receiver provides a Java RMI distributed Receiver object in the Message
 * Queuing system. java Start Receiver <host> <port> <servername> <receiverid>
 * <host> is the name of the host computer where the Registry Server is running.
 * <port> is the port number to which the Registry Server is listening.
 * <servername> is the MQ server's name. <receiverid> is the receiver's ID,
 * which must be an integer.
 */
public class Receiver implements ReceiverRef {
	private String host;
	private int port;
	private String servername;
	private int receiverid;

	private RegistryProxy registry;

	/**
	 * Construct a new Reciver object.
	 * <P>
	 * The command line arguments are: <BR>
	 * <TT>args[0]</TT> = Registry Server's host <BR>
	 * <TT>args[1]</TT> = Registry Server's port <BR>
	 * <TT>args[2]</TT> = name of the message queuing server, where this
	 * Receivers queue is stored <BR>
	 * <TT>args[3]</TT> = ID of this receiver
	 * 
	 * @param args
	 *            Command line arguments.
	 * @throws NotBoundException
	 * @throws RemoteException
	 * @throws Exception
	 * 
	 * @exception IllegalArgumentException
	 *                (unchecked exception) Thrown if there was a problem with
	 *                the command line arguments.
	 */
	public Receiver(String[] args) throws NotBoundException, RemoteException {
		// Parse command line arguments.
		if (args.length != 4) {
			throw new IllegalArgumentException(
					"Incorrect number of arguments.\nUsage: java Start Receiver <host> <port> <servername> <receiverid>");
		}
		host = args[0];
		port = parseInt(args[1], "port");
		servername = args[2];
		receiverid = parseInt(args[3], "recieverid");
		// Get a proxy for the Registry Server.
		try {
		registry = new RegistryProxy(host, port);
		} catch (RemoteException e) {
			throw new RemoteException(
					"There is no Registry server running at given host and port");
		}
		//make sure messaging queue server exists
		try {
			registry.lookup(servername);
		} catch (NotBoundException exc) {
			throw new NotBoundException("MQServer with given name does not exist");
		} catch (RemoteException e) {
			throw e;
		}
		// Export this Receiver.
		UnicastRemoteObject.exportObject(this, 0);

		// Bind this Receiver into the Registry Server.
		try {
			registry.bind(servername + receiverid, this, 500);
		} catch (AlreadyBoundException exc) {
			try {
				UnicastRemoteObject.unexportObject(this, true);
			} catch (NoSuchObjectException exc2) {
			}
			throw new IllegalArgumentException("Reciver(): <servername><reciverid> = \""
					+servername+ receiverid + "\" already exists\n Same Receiver ID can be used, but different MQServer has to be used");
		} catch (RemoteException exc) {
			try {
				UnicastRemoteObject.unexportObject(this, true);
			} catch (NoSuchObjectException exc2) {
			}
			throw exc;
		}
	}

	/**
	 * Parse an integer command line argument.
	 * 
	 * @param arg
	 *            Command line argument.
	 * @param name
	 *            Argument name.
	 * 
	 * @return Integer value of <TT>arg</TT>.
	 * 
	 * @exception IllegalArgumentException
	 *                (unchecked exception) Thrown if <TT>arg</TT> cannot be
	 *                parsed as an integer.
	 */
	private static int parseInt(String arg, String name) {
		try {
			return Integer.parseInt(arg);
		} catch (NumberFormatException exc) {
			throw new IllegalArgumentException("Receiver(): Invalid <" + name
					+ ">: \"" + arg + "\"");
		}
	}

	/**
	 * Print the message the receiver receives and where it came from
	 * 
	 * @param senderID - ID of the sender,
	 * @param Message - Message that the sender sent
	 * 
	 */
	public void loadMessage(final int senderID, final String message) {
		System.out.printf("From %s: \"%s\"\n", senderID, message);


	}
	/**
	 * @return id of this receiver
	 */
	public int getID() {
		return this.receiverid;
	}

}
